#ifndef CONFIG_H
#define CONFIG_H

#include <optional>
#include <QString>
#include <QVector>
#include <QPoint>
#include <QJsonArray>
#include <QJsonDocument>

class Config
{
private:
    static Config* instance;
    Config();
public:
    static Config* Instance()
    {
        if (instance == nullptr)
            instance = new Config();
        return instance;
    }
private:
    std::optional<QVector<QString>> models = std::nullopt;
    std::optional<QString> defaultModel = std::nullopt;
    std::optional<QPoint> windowCreateOffset = std::nullopt;
    QJsonDocument config;
public:
    [[nodiscard]] inline const QVector<QString>& GetModelList()
    {
        if (models != std::nullopt)
            return models.value();
        auto array = config["models"].toArray();
        auto tmp = QVector<QString>(array.size());
        for(int i = 0; i < array.size(); i++)
            tmp[i] = array[i].toString();
        models = tmp;
        return models.value();
    }

    [[nodiscard]] inline const QString& GetDefaultModel()
    {
        if (defaultModel != std::nullopt)
            return defaultModel.value();
        defaultModel = config["defaultModel"].toString();
        return defaultModel.value();
    }

    [[nodiscard]] inline const QPoint& GetWindowCreateOffset()
    {
        if (windowCreateOffset != std::nullopt)
            return windowCreateOffset.value();
        auto offset = config["windowCreateOffset"].toArray();
        windowCreateOffset = QPoint(offset[0].toInt(), offset[1].toInt());
        return windowCreateOffset.value();
    }
};


#endif //CONFIG_H
